package gui;

import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;

import client.ClientConsole;

import logic.Message;
import logic.Reader;

/** 
 * Shows freeze reader form and delivers user's input.
 * Freezes a reader chosen by the library manager
 */
public class FreezeReaderForm extends JPanel implements ActionListener{

	private JPanel mainPanel,searchPanel,centerPanel,bottomPanel;
	private JTextField IDReaderNumber;
	private JButton freezeReader;
	private JButton back;
	/** confirmation label shows feedback from the server */
	private JLabel confirmationLabel,IDNumber;
	
	/**Default constructor- Builds the freeze reader form*/
	public FreezeReaderForm()
	{
		super();
		setSize(300,300);
		mainPanel=new JPanel();
		mainPanel.setLayout(new BorderLayout());
		searchPanel=new JPanel();
		IDNumber=new JLabel("ID Number");
		IDReaderNumber=new JTextField(20);
		freezeReader=new JButton ("freeze Reader");
		searchPanel.add(IDNumber);
		searchPanel.add(IDReaderNumber);
		searchPanel.add(freezeReader);
		mainPanel.add(searchPanel,BorderLayout.NORTH);
		centerPanel=new JPanel();
		confirmationLabel=new JLabel();
		confirmationLabel.setVisible(false);
		centerPanel.add(confirmationLabel);
		mainPanel.add(centerPanel,BorderLayout.CENTER);
		bottomPanel=new JPanel();
		back=new JButton("back");
		bottomPanel.add(back);
		mainPanel.add(bottomPanel,BorderLayout.SOUTH);
		add(mainPanel);
		freezeReader.addActionListener(this);
		back.addActionListener(this);		
	
	}
	//Handles user's clicks on buttons
	@Override
	public void actionPerformed(ActionEvent ae) {
		if (ae.getSource()==back)
		{
			this.setVisible(false);
			ClientConsole.aFrame.setContentPane(new LibraryManagerWindow());
		}
		
		else if (ae.getSource()==freezeReader)
		{
			if (IDReaderNumber.getText().isEmpty())
			{
				confirmationLabel.setText("Please fill all fields");
				confirmationLabel.setVisible(true);
			}
			else
			{
				Message m;
				m=new Message("Freeze Reader Card",new Reader(Integer.parseInt(IDReaderNumber.getText())));
				ClientConsole.HandleMessageFromGui(m);
				
				m=(Message)ClientConsole.client.getMessage();
				
				if(m.getCommand().equals("The Reader is frozen successfully"))	
				{
					confirmationLabel.setText("Reader frozed successfully");
					confirmationLabel.setVisible(true);	
				}
				else{
					confirmationLabel.setText(m.getCommand());
					confirmationLabel.setVisible(true);
				}
			}
		}
	}
			
}
